#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

#define MAXN 5000

char str[MAXN], *suffix[MAXN];
int len;

int comlen(const char *p1, const char *p2) {
    int i = 0;
    while(p1 && p2 && (*p1++ == *p2++))
        i++;
    return i;
}

/* 注意参数 如何对一个字符串指针数组进行排序 */
int pstrcmp(const void *p1, const void *p2) {
    int a = *(const int *)p1;
    int b = *(const int *)p2;
    return strcmp((const char *)a, (const char *)b);
}

int main() {
    strcpy(str, "abcabcd");
    len = strlen(str);
    cout << "len: " << len << endl;

    for (int i = 0; i < len; ++i) {
        suffix[i] = &str[i];
    }

    qsort(suffix, len, sizeof(char *), pstrcmp);

    for (int i = 0; i < len; ++i) {
        cout << suffix[i] << endl;
    }

    int maxlen = 0;
    for (int i = 0; i < len-1; ++i) {
        int temp = comlen(suffix[i], suffix[i+1]);
        if (maxlen < temp) {
            maxlen = temp;
        }
    }
    cout << "maxlen: " << maxlen << endl;

    return 0;
}
